package com.cdu.guoxinan.www.smartcom.controller.propertyController;

import com.cdu.guoxinan.www.smartcom.aop.Annotation.OperationLog;
import com.cdu.guoxinan.www.smartcom.domian.dto.AddRepairDTO;
import com.cdu.guoxinan.www.smartcom.domian.dto.QueryRepairDTO;
import com.cdu.guoxinan.www.smartcom.domian.dto.RepairDTO;
import com.cdu.guoxinan.www.smartcom.domian.dto.RequestDTO;
import com.cdu.guoxinan.www.smartcom.domian.pojo.Repair;
import com.cdu.guoxinan.www.smartcom.domian.vo.RepairVO;
import com.cdu.guoxinan.www.smartcom.service.RepairApplyService;
import com.cdu.guoxinan.www.smartcom.service.RepairService;
import com.cdu.guoxinan.www.smartcom.unit.model.R;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/repair")
@Api(tags = "报修管理")
public class RepairController {

    @Resource
    private RepairService repairService;

    @Resource
    private RepairApplyService repairApplyService;


    @GetMapping("/all")
    @ApiModelProperty("获取所有报修记录")
    @OperationLog(description = "获取所有报修记录")
    public R getAllRepairs(QueryRepairDTO queryRepairDTO) {
        PageInfo<RepairVO> allRepairs = repairService.getAllRepairs(queryRepairDTO);
        return R.ok(allRepairs);
    }

    @PostMapping("/add")
    @ApiModelProperty("新增报修记录")
    @OperationLog(description = "新增报修记录")
    @Transactional(rollbackFor = Exception.class)
    public R addRepair(@RequestBody AddRepairDTO addRepairDTO) {

       RepairDTO repairDTO = new RepairDTO();

       repairDTO.setCompletedAt(addRepairDTO.getCompletedAt());
       repairDTO.setRepairDescription(addRepairDTO.getRepairDescription());
       repairDTO.setCost(addRepairDTO.getCost());
       repairDTO.setIdCard(addRepairDTO.getIdCard());
       repairDTO.setRequestId(addRepairDTO.getRequestId());
       repairDTO.setTechnicianId(addRepairDTO.getId());

        RequestDTO requestDTO = new RequestDTO();
        requestDTO.setStatus(addRepairDTO.getStatus());
         requestDTO.setRequestId(repairDTO.getRequestId());
        int i=repairService.addRepair(repairDTO);
        if (i==-1){
            return R.error("报修处理失败");
        }



        int j=repairApplyService.updateRepairApply(requestDTO);

        System.out.println("j:  "+j);

        return j>0 ? R.ok("处理成功") : R.error("处理失败");
    }

    @PostMapping("/update")
    @ApiModelProperty("更新报修记录")
    @OperationLog(description = "更新报修记录")
    public R updateRepair(@RequestBody Repair repair)
    {
        int i=repairService.updateRepair(repair);
        System.out.println("sss:  "+i);
        if (i==-1){
            return R.error("请先处理完成报修任务！");
        }

        return  R.ok("跟新成功");
    }

    @PostMapping("/delete")
    @ApiModelProperty("删除报修记录")
    @OperationLog(description = "删除报修记录")
    public R deleteRepair(@RequestParam Long recordId) {
        return repairService.deleteRepair(recordId) > 0 ? R.ok("删除成功") : R.error("删除失败");
    }

    @PostMapping("/deleteBatch")
    @ApiModelProperty("批量删除报修记录")
    @OperationLog(description = "批量删除报修记录")
    public R deleteBatch(@RequestBody Integer[] ids) {
        return repairService.deleteBatch(ids) > 0 ? R.ok("删除成功") : R.error("删除失败");
    }
}
